<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for configuration feature of the component
*/
class JobBoardControllerJaapplications extends JobBoardController{

    /**
     * Constructor
     */
    function __construct( $default = array() )
    {
    	$layout = JRequest::getVar('layout');
        if ($layout=='download')
        	JRequest::setVar('task','download');
        parent::__construct( $default );
        $this->set('name', 'applications');
        $this->set('fullname', JBPREFIX.'applications');
        
        // Register Extra tasks
        $this->registerTask( 'add',        'edit' );
        $this->registerTask( 'apply',    'save' );
        $this->registerTask( 'inactivate',    'activate' );
    }
    
    /**
    * Display current configuration of the component to administrator
    * 
    */
    function display(){
    	$user = JFactory::getUser();
        if ($user->id==0)
        {
        	$this->setRedirect(JRoute::_("index.php?option=com_user&view=login"));
        	return ;
        }
		JRequest::setVar('view', $this->get('fullname')); // Force using associated view
        parent::display();
		
    }
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
        $Itemid = JRequest::getVar('Itemid');
        $this->setRedirect(JRoute::_("index.php?option=". JBCOMNAME ."&view=". $this->get('fullname')."&Itemid=".$Itemid));
    }
    
    /**
    * Remove a configuration row
    * 
    */
    function remove(){
    	// Check for request forgeries
		// JRequest::checkToken() or jexit( 'Invalid Token' );
        $task = JRequest::getVar('task', 'remove');
        $limited = JRequest::getVar('limited', 0);
        $Itemid = JRequest::getVar('Itemid');
        $model =& $this->getModel($this->get('fullname'));
        if($n = $model->remove($task)){
		    $this->setMessage(JText::sprintf('Items removed', $n));
		    $this->setRedirect(JRoute::_('index.php?option=com_jajobboard&view=jaapplications&layout=jaempllist&Itemid='.$Itemid));
        } else {
            echo "<script>alert('".JText::_('Could not remove the application')."'); window.history.go(-1);</script>";
        }
	}
    
	
	/**
	* Save configuration record
	*/
	function save(){
        // Check for request forgeries
        $step = JRequest::getVar('step', ''); 
        ($step == 'done') or JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $Itemid = JRequest::getVar('Itemid');                 

        $model    =& $this->getModel( $this->get('fullname') );
        
        $rc = $model->store();
        if($rc){
            $item = $model->getItem();
            $msg = JText::_( 'You have successfully applied for this job' );   
            return $item->job_id;
        }
        else 
        {
//        	JError::raiseWarning(0,JText::_('Have error when applying job'));
			JRequest::setVar('app_postback',true);
        	return false;
        }
	}
    function download(){
         $model    =& $this->getModel( $this->get('fullname') );    
         $model->download();
         exit;  
    }
    
    function remove_attach()
    {
    	$model    =& $this->getModel( $this->get('fullname') );
    	$model->remove_attach();
         exit;
    }
    function sendMailAsRead()
    {
    	$model =& $this->getModel($this->get('fullname'));
    	$model->sendmailAsReadMail();
    }
    function empployer_remove()
    {
    	$model    =& $this->getModel( $this->get('fullname') );
    	$model->empployer_remove();
    	$this->setRedirect( JRoute::_('index.php?option=com_jajobboard&view=jaapplications&layout=jaempllist&Itemid='.JRequest::getInt('Itemid')), JText::_('Successfully removed your applications') );
    }
    function jobseeker_remove()
    {
    	$model    =& $this->getModel( $this->get('fullname') );
    	$model->jobseeker_remove();
    	$this->setRedirect( JRoute::_('index.php?option=com_jajobboard&view=jaapplications&layout=my-applications&Itemid='.JRequest::getInt('Itemid')), JText::_('Successfully removed your applications') );
    }
}
?>
